<?php
namespace app\api\controller\v1;
use app\api\model\Notice;
use think\facade\Cache;
use app\api\validate\NoticeValidate;

class App extends Common
{
    public function addNotice(){
        $data = request()->param();
        $validate = new NoticeValidate;
        //验证必须有班级ids
        $images = isset($data['images'])?implode(",", $data['images']):"";
        $content = isset($data['content'])?$data['content']:"";
        if (!$validate->scene('app_validate')->check($data)) {
            return show(1, $validate->getError(), 0 , 200);
        }
        //验证内容和图片至少有一项
        if(!$images && !$content){
            //验证内容和图片至少有一项不能为空
            return show(1, "内容和图集至少有一项", 0 , 200);
        }
        $ndata = [
           'title'=> $data['title'],
           'description'=>  mb_substr($content,100),
           'created_at'=> date('Y-m-d H:i:s', time()),
           'start_time'=> date('Y-m-d H:i:s', time()),
           'end_time'=> date('Y-m-d H:i:s', time()),
           'sort'=> 100,
           'show'=> 1,
           'class_ids'=> implode(",", $data['class_ids']),
           'content'=> $content,
           'user_id'=>$data['user_id'],
           'images'=> $images,
           'platform'=>$data['platform'],
           'school_id'=>$data['school_id'],
        ];
        Cache::clear('notice');
        $rest = (new Notice())->strict(false)->insert($ndata);
        //推送key;
        $pushKey = [
            'action' => 'refresh',
            'params' => 'notice',
        ];
        if($rest){
            //如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
            $this->socketPush($data['class_ids'],$pushKey);
        }
        return show(1, '添加成功', $rest, 200);

    }
}